﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
namespace HY.DAL.co
{
    /// <summary>
    /// 数据访问类:pub_tel
    /// </summary>
    public partial class pub_tel
    {
       
   public pub_tel()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int sid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from pub_tel");
			strSql.Append(" where sid=@sid");
			SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
			parameters[0].Value = sid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HY.Model.co.pub_tel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into pub_tel(");
			strSql.Append("bh,bhname,person,job,ctel,mtel,sex,qq,msn)");
			strSql.Append(" values (");
			strSql.Append("@bh,@bhname,@person,@job,@ctel,@mtel,@sex,@qq,@msn)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@bh", SqlDbType.NVarChar,100),
					new SqlParameter("@bhname", SqlDbType.NVarChar,200),
					new SqlParameter("@person", SqlDbType.NVarChar,100),
					new SqlParameter("@job", SqlDbType.NVarChar,100),
					new SqlParameter("@ctel", SqlDbType.NVarChar,100),
					new SqlParameter("@mtel", SqlDbType.NVarChar,100),
					new SqlParameter("@sex", SqlDbType.NVarChar,100),
					new SqlParameter("@qq", SqlDbType.NVarChar,100),
					new SqlParameter("@msn", SqlDbType.NVarChar,100)};
			parameters[0].Value = model.bh;
			parameters[1].Value = model.bhname;
			parameters[2].Value = model.person;
			parameters[3].Value = model.job;
			parameters[4].Value = model.ctel;
			parameters[5].Value = model.mtel;
			parameters[6].Value = model.sex;
			parameters[7].Value = model.qq;
			parameters[8].Value = model.msn;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HY.Model.co.pub_tel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update pub_tel set ");
			strSql.Append("bh=@bh,");
			strSql.Append("bhname=@bhname,");
			strSql.Append("person=@person,");
			strSql.Append("job=@job,");
			strSql.Append("ctel=@ctel,");
			strSql.Append("mtel=@mtel,");
			strSql.Append("sex=@sex,");
			strSql.Append("qq=@qq,");
			strSql.Append("msn=@msn");
			strSql.Append(" where sid=@sid");
			SqlParameter[] parameters = {
					new SqlParameter("@bh", SqlDbType.NVarChar,100),
					new SqlParameter("@bhname", SqlDbType.NVarChar,200),
					new SqlParameter("@person", SqlDbType.NVarChar,100),
					new SqlParameter("@job", SqlDbType.NVarChar,100),
					new SqlParameter("@ctel", SqlDbType.NVarChar,100),
					new SqlParameter("@mtel", SqlDbType.NVarChar,100),
					new SqlParameter("@sex", SqlDbType.NVarChar,100),
					new SqlParameter("@qq", SqlDbType.NVarChar,100),
					new SqlParameter("@msn", SqlDbType.NVarChar,100),
					new SqlParameter("@sid", SqlDbType.Int,4)};
			parameters[0].Value = model.bh;
			parameters[1].Value = model.bhname;
			parameters[2].Value = model.person;
			parameters[3].Value = model.job;
			parameters[4].Value = model.ctel;
			parameters[5].Value = model.mtel;
			parameters[6].Value = model.sex;
			parameters[7].Value = model.qq;
			parameters[8].Value = model.msn;
			parameters[9].Value = model.sid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int sid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pub_tel ");
			strSql.Append(" where sid=@sid");
			SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
			parameters[0].Value = sid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string sidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pub_tel ");
			strSql.Append(" where sid in ("+sidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HY.Model.co.pub_tel GetModel(int sid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 sid,bh,bhname,person,job,ctel,mtel,sex,qq,msn from pub_tel ");
			strSql.Append(" where sid=@sid");
			SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
			parameters[0].Value = sid;

			HY.Model.co.pub_tel model=new HY.Model.co.pub_tel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["sid"]!=null && ds.Tables[0].Rows[0]["sid"].ToString()!="")
				{
					model.sid=int.Parse(ds.Tables[0].Rows[0]["sid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["bh"]!=null && ds.Tables[0].Rows[0]["bh"].ToString()!="")
				{
					model.bh=ds.Tables[0].Rows[0]["bh"].ToString();
				}
				if(ds.Tables[0].Rows[0]["bhname"]!=null && ds.Tables[0].Rows[0]["bhname"].ToString()!="")
				{
					model.bhname=ds.Tables[0].Rows[0]["bhname"].ToString();
				}
				if(ds.Tables[0].Rows[0]["person"]!=null && ds.Tables[0].Rows[0]["person"].ToString()!="")
				{
					model.person=ds.Tables[0].Rows[0]["person"].ToString();
				}
				if(ds.Tables[0].Rows[0]["job"]!=null && ds.Tables[0].Rows[0]["job"].ToString()!="")
				{
					model.job=ds.Tables[0].Rows[0]["job"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ctel"]!=null && ds.Tables[0].Rows[0]["ctel"].ToString()!="")
				{
					model.ctel=ds.Tables[0].Rows[0]["ctel"].ToString();
				}
				if(ds.Tables[0].Rows[0]["mtel"]!=null && ds.Tables[0].Rows[0]["mtel"].ToString()!="")
				{
					model.mtel=ds.Tables[0].Rows[0]["mtel"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sex"]!=null && ds.Tables[0].Rows[0]["sex"].ToString()!="")
				{
					model.sex=ds.Tables[0].Rows[0]["sex"].ToString();
				}
				if(ds.Tables[0].Rows[0]["qq"]!=null && ds.Tables[0].Rows[0]["qq"].ToString()!="")
				{
					model.qq=ds.Tables[0].Rows[0]["qq"].ToString();
				}
				if(ds.Tables[0].Rows[0]["msn"]!=null && ds.Tables[0].Rows[0]["msn"].ToString()!="")
				{
					model.msn=ds.Tables[0].Rows[0]["msn"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select sid,bh,bhname,person,job,ctel,mtel,sex,qq,msn ");
			strSql.Append(" FROM pub_tel ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" sid,bh,bhname,person,job,ctel,mtel,sex,qq,msn ");
			strSql.Append(" FROM pub_tel ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM pub_tel ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.sid desc");
			}
			strSql.Append(")AS Row, T.*  from pub_tel T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "pub_tel";
			parameters[1].Value = "sid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}

}

